{template 'common/header'}
{php echo $this -> set_tabbar($action, 4);}
{if $operation == 'display'}
<div class="main">
    <form action="" method="post" class="form-horizontal form">
        <div class="panel panel-default">
            <div class="panel-body">
                <a class="btn btn-primary" href="{php echo $this->createWebUrl('exchange', array('op' => 'post'))}"><i class="fa fa-plus"></i> 添加积分兑换</a>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="table-responsive panel-body">
                <table class="table table-hover">
                    <thead class="navbar-inner">
                    <tr>
                        <th style="width:10%;">显示顺序</th>
                        <th style="width:30%;">标题</th>
                        <th style="width:10%;">所需积分</th>
                        <th style="width:25%;">时间</th>
                        <th style="width:10%;">状态</th>
                        <th style="width:15%;">记录/编辑/删除</th>
                    </tr>
                    </thead>
                    <tbody id="level-list">
                    {loop $exchanges $exchange}
                    <tr>
                        <td><input type="text" class="form-control" name="displayorder[{$exchange['id']}]" value="{$exchange['displayorder']}"></td>
                        <td>{$exchange['title']}</td>
                        <td>{$exchange['needscore']}</td>
                        <td>
                            生效时间：{php echo date('Y-m-d H:i:s', $exchange[starttime]);}<br/>
                            过期时间：{php echo date('Y-m-d H:i:s', $exchange[endtime]);}
                        </td>
                        <td>
                            {if TIMESTAMP<$exchange['starttime']}
                            <span class="label label-danger">未开始</span>
                            {elseif TIMESTAMP>$exchange['starttime'] && TIMESTAMP<$exchange['endtime']}
                            <span class="label label-success">进行中</span>
                            {else}
                            <span class="label label-danger">已结束</span>
                            {/if}
                        </td>
                        <td>
                            <a class="btn btn-default btn-sm" href="{php echo $this->createWebUrl('sncodelist', array('op' => 'display', 'type' => $exchange['ticket_ty'], 'id' => $exchange['ticket_id']))}"><i class="fa fa-bar-chart"></i></a>
                            <a class="btn btn-default btn-sm" href="{php echo $this->createWebUrl('exchange', array('op' => 'post', 'id' => $exchange['id']))}"><i class="fa fa-pencil"></i></a>
                            <a class="btn btn-default btn-sm" onclick="return confirm('确认删除吗？');return false;" href="{php echo $this->createWebUrl('exchange', array('op' => 'delete', 'id' => $exchange['id']))}"><i class="fa fa-times"></i></a>
                        </td>
                    </tr>
                    {/loop}
                    </tbody>
                    <tfoot>
                    <tr>
                        <td colspan="7">
                            <input name="submit" type="submit" class="btn btn-primary" value="批量排序">
                            <input type="hidden" name="token" value="{$_W['token']}" />
                        </td>
                    </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </form>
    {$pager}
</div>
{else if $operation == 'post'}
<div class="main">
    <form action="" method="post" class="form-horizontal form" enctype="multipart/form-data">
        <input type="hidden" id="id" name="id" value="{$reply['id']}"/>
        <div class="panel panel-default">
            <div class="panel-heading">
                {$title}
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">活动名称</label>
                    <div class="col-sm-9">
                        <input type="text" name="title" value="{$reply['title']}" id="title" class="form-control" placeholder="请输入活动名称"/>
                        <div class="help-block">不超过30个字符</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">活动开始时间</label>
                    <div class="col-sm-9">
                        {php echo tpl_form_field_date('starttime', $starttime, true)}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">活动结束时间</label>
                    <div class="col-sm-9">
                        {php echo tpl_form_field_date('endtime', $endtime, true)}
                    </div>
                </div>
                <div class="form-group" id="level">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">选择人群</label>
                    <div class="col-sm-9">
                        <label class="radio-inline">
                            <input type="radio" name="levelid" value="0" {if empty($reply)}checked="checked"{elseif $reply['levelid']==0}checked="checked"{/if} />所有会员
                        </label>
                        {loop $levels $level}
                        <label class="radio-inline">
                            <input type="radio" name="levelid" value="{$level['id']}" {if $reply['levelid']==$level['id']}checked="checked"{/if} >{$level['levelname']}
                        </label>
                        {/loop}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">活动</label>
                    <div class="col-sm-3">
                        <select name="ticket_ty" id="ticket_ty" selectedindex="0"   onchange="fetchChildCategory(this.options[this.selectedIndex].value)" autocomplete="off" class="form-control">
                            <option value="0">请选择</option>
                            <option value="1"{if $reply['ticket_ty']==1} selected{/if}>优惠券</option>
                            <option value="2"{if $reply['ticket_ty']==2} selected{/if}>代金券</option>
                            <option value="3"{if $reply['ticket_ty']==3} selected{/if}>礼品券</option>
                        </select>
                    </div>
                    <div class="col-sm-3">
                        <select name="ticket_id" id="cate_2" selectedindex="0" class="form-control">
                            <option value="0">请选择</option>
                            {if !empty($children[$reply['ticket_ty']])}
                            {loop $children[$reply['ticket_ty']] $row}
                            <option value="{$row[0]}" {if $row[0] == $reply['ticket_id']} selected="selected"{/if}>{$row[1]}</option>
                            {/loop}
                            {/if}
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">兑换所需积分</label>
                    <div class="col-sm-9">
                        <input type="text" name="needscore" value="{if empty($reply['needscore'])}0{else}{$reply['needscore']}{/if}" id="needscore" class="form-control" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">发送总数</label>
                    <div class="col-sm-9">
                        <input type="text" name="totalcount" value="{if empty($reply['totalcount'])}0{else}{$reply['totalcount']}{/if}" id="totalcount" class="form-control" />
                        <div class="help-block" style="color:#f00;">0表示不限</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">每个用户可领取数</label>
                    <div class="col-sm-9">
                        <input type="text" name="count" value="{if empty($reply['count'])}0{else}{$reply['count']}{/if}" id="count" class="form-control" />
                        <div class="help-block" style="color:#f00;">0表示不限</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">排序</label>
                    <div class="col-sm-9">
                        <input type="text" name="displayorder" value="{if empty($reply)}0{else}{$reply['displayorder']}{/if}" id="displayorder" class="form-control" />
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-12">
                <button type="submit" class="btn btn-primary col-lg-1" name="submit" value="提交">保存设置</button>
                <input type="hidden" name="token" value="{$_W['token']}"/>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    function check() {
        if($.trim($('#title').val()) == '') {
            message('没有输入标题.', '', 'error');
            return false;
        }
        if($.trim($('#title').val()).length > 30) {
            message('标题不能多于30个字.', '', 'error');
            return false;
        }
        var count = $.trim($('#count').val());
        if(count == '') {
            message('没有输入积分兑换张数.', '', 'error');
            return false;
        }
        if(isNaN(count)){
            message('积分兑换张数必须为数字.', '', 'error');
            return false;
        }
        return true;
    }
</script>
<script type="text/javascript">
    kindeditor($('.richtext-clone'));
    kindeditorUploadBtn($('#btnpicture'));
</script>
<script type="text/javascript">
    $('#date-range').daterangepicker({
        format: 'YYYY-MM-DD H:m',
        startDate: $(':hidden[name=starttime]').val(),
        endDate: $(':hidden[name=endtime]').val(),
        startTime:$(':hidden[name=starttime]').val(),
        endTime:$(':hidden[name=endtime]').val(),
        locale: {
            applyLabel: '确定',
            cancelLabel: '取消',
            fromLabel: '从',
            toLabel: '至',
            weekLabel: '周',
            customRangeLabel: '日期范围',
            daysOfWeek: moment()._lang._weekdaysMin.slice(),
            monthNames: moment()._lang._monthsShort.slice(),
            firstDay: 0
        }
    }, function(start, end){
        $('#date-range .date-title').html(start.format('YYYY-MM-DD H:m') + ' 至 ' + end.format('YYYY-MM-DD H:m'));
        $(':hidden[name=starttime]').val(start.format('YYYY-MM-DD H:m'));
        $(':hidden[name=endtime]').val(end.format('YYYY-MM-DD H:m'));
    });
</script>
<script type="text/javascript" src="./resource/script/kindeditor/kindeditor-min.js"></script>
<script type="text/javascript" src="./resource/script/kindeditor/lang/zh_CN.js"></script>
<link type="text/css" rel="stylesheet" href="./resource/script/kindeditor/themes/default/default.css" />
<script>
    var editor = KindEditor.editor({
        allowFileManager : true,
        uploadJson : "./index.php?act=attachment&do=upload",
        fileManagerJson : "./index.php?act=attachment&do=manager",
        afterUpload : function(url, data) {
        }
    });
    $("#upload-image-thumb").click(function() {
        editor.loadPlugin("image", function() {
            editor.plugin.imageDialog({
                tabIndex : 1,
                imageUrl : $("#upload-image-url-thumb").val(),
                clickFn : function(url) {
                    editor.hideDialog();
                    var val = url;
                    if(url.toLowerCase().indexOf("http://") == -1 && url.toLowerCase().indexOf("https://") == -1) {
                        var filename = /images(.*)/.exec(url);
                        if(filename && filename[0]) {
                            val = filename[0];
                        }
                    }
                    $("#upload-image-url-thumb-old").val($("#upload-image-url-thumb").val());
                    $("#upload-image-url-thumb").val(val);
                    $("#upload-image-preview-thumb").html('<img src="'+url+'" width="80px;" />');
                }
            });
        });
    });
</script>

<script type="text/javascript">
    function fetchChildCategory(cid) {
        var html = '<option value="0">请选择</option>';
        if (!category || !category[cid]) {
            html += '<option value="-1">未找到券</option>';
            $('#cate_2').html(html);
            return false;
        }
        for (i in category[cid]) {
            html += '<option value="'+category[cid][i][0]+'">'+category[cid][i][1]+'</option>';
        }
        $('#cate_2').html(html);
    }
    <!--
    var category = {php echo json_encode($children)};
    //-->
</script>
{/if}
{template 'common/footer'}
